<!DOCTYPE html>
<html  lang="en">
<head>
    <meta charset="utf-8">
<title>keepalive+lvs 四层负载均衡 - noback</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />



    <meta name="description" content="关键词LB (Load Balancer 负载均衡)HA (High Available 高可用)Failover (失败切换)Cluster (集群)LVS (Linux Virtual Server Linux 虚拟服务器)DS (Director Server)，指的是前端负载均衡器节点RS (Real Server)，后端真实的工作服务器VIP (Virtual IP)，虚拟的 IP 地址">
<meta property="og:type" content="article">
<meta property="og:title" content="keepalive+lvs 四层负载均衡">
<meta property="og:url" content="http://alpaca-h.gitee.io/2020/01/05/blog_back_new/linux/keepalive+lvs%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/index.html">
<meta property="og:site_name" content="noback">
<meta property="og:description" content="关键词LB (Load Balancer 负载均衡)HA (High Available 高可用)Failover (失败切换)Cluster (集群)LVS (Linux Virtual Server Linux 虚拟服务器)DS (Director Server)，指的是前端负载均衡器节点RS (Real Server)，后端真实的工作服务器VIP (Virtual IP)，虚拟的 IP 地址">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://alpaca-h.gitee.io/images/og_image.png">
<meta property="article:published_time" content="2020-01-05T05:06:09.000Z">
<meta property="article:modified_time" content="2020-01-07T03:42:42.162Z">
<meta property="article:author" content="Alpaca">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://alpaca-h.gitee.io/images/og_image.png">







<link rel="icon" href="/images/favicon.svg">


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/androidstudio.css">


    
    
    
    <style>body>.footer,body>.navbar,body>.section{opacity:0}</style>
    

    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css">
    

    
    

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css">


    
    
    
    

<link rel="stylesheet" href="/css/back-to-top.css">


    
    

    
    
    
    

    
    
<link rel="stylesheet" href="/css/progressbar.css">
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

    
    
    

    
    
    
        <script async="" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    

    


<link rel="stylesheet" href="/css/style.css">
<meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/atom.xml" title="noback" type="application/atom+xml">
</head>
<body class="is-3-column">
    <nav class="navbar navbar-main">
    <div class="container">
        <div class="navbar-brand is-flex-center">
            <a class="navbar-item navbar-logo" href="/">
            
                <img src="/images/logo.svg" alt="keepalive+lvs 四层负载均衡" height="28">
            
            </a>
        </div>
        <div class="navbar-menu">
            
            <div class="navbar-start">
                
                <a class="navbar-item"
                href="/">Home</a>
                
                <a class="navbar-item"
                href="/archives/">Archives</a>
                
                <a class="navbar-item"
                href="/categories/">Categories</a>
                
                <a class="navbar-item"
                href="/tags/">Tags</a>
                
                <a class="navbar-item"
                href="/about/">About</a>
                
            </div>
            
            <div class="navbar-end">
                
                    
                    
                    <a class="navbar-item" target="_blank" title="AlphaLxy GitHub" href="https://www.github.com/AlphaLxy">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                    
                
                
                <a class="navbar-item is-hidden-tablet catalogue" title="Catalogue" href="javascript:;">
                    <i class="fas fa-list-ul"></i>
                </a>
                
                
                <a class="navbar-item search" title="Search" href="javascript:;">
                    <i class="fas fa-search"></i>
                </a>
                
            </div>
        </div>
    </div>
</nav>
    
    <section class="section">
        <div class="container">
            <div class="columns">
                <div class="column is-8-tablet is-8-desktop is-9-widescreen has-order-2 column-main"><div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <i class="fas fa-angle-double-right"></i>keepalive+lvs 四层负载均衡
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2020-01-05T05:06:09.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2020-01-05</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2020-01-07T03:42:42.162Z"><i class="far fa-calendar-check">&nbsp;</i>2020-01-07</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/linux/">linux</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    22 minutes read (About 3304 words)
                </span>
                
                
                <span class="level-item has-text-grey" id="busuanzi_container_page_pv">
                    <i class="far fa-eye"></i>
                    <span id="busuanzi_value_page_pv">0</span> visits
                </span>
                
            </div>
        </div>
        
        <div class="content">
            <h2 id="关键词"><a href="#关键词" class="headerlink" title="关键词"></a>关键词</h2><p>LB (Load Balancer 负载均衡)<br>HA (High Available 高可用)<br>Failover (失败切换)<br>Cluster (集群)<br>LVS (Linux Virtual Server Linux 虚拟服务器)<br>DS (Director Server)，指的是前端负载均衡器节点<br>RS (Real Server)，后端真实的工作服务器<br>VIP (Virtual IP)，虚拟的 IP 地址，向外部直接面向用户请求，作为用户请求的目标的 IP 地址<br>DIP (Director IP)，主要用于和内部主机通讯的 IP 地址<br>RIP (Real Server IP)，后端服务器的 IP 地址<br>CIP (Client IP)，访问客户端的 IP 地址</p>
<a id="more"></a>
<h2 id="仅使用lvs"><a href="#仅使用lvs" class="headerlink" title="仅使用lvs"></a>仅使用lvs</h2><p>单节点lvs 、 vip  两台RS服务器</p>
<pre><code class="text">vip: 10.0.5.97/20
lvs-director: 10.0.5.92

RS1: 10.0.5.90
RS2: 10.0.5.91</code></pre>
<p>在lvs上进行操作</p>
<pre><code class="bash"># 安装lvs
yum install ipvsadm -y 
# 安装net-tools
yum install net-tools


# 在网卡上绑定vip
ifconfig eno1:0 10.0.5.97 broadcast 10.0.5.97 netmask 255.255.240.0 up
# 添加路由
route add -host 10.0.5.97 dev eno1
# 启用系统的包转发功能
echo &quot;1&quot; &gt; /proc/sys/net/ipv4/ip_forward
# 清空系统之前所有的ipvsadm规则
ipvsadm --clear
ipvsadm -C
# 增加虚拟ip (-s rr 表述轮询)
ipvsadm -A -t 10.0.5.97:8888 -s rr
# ipvsadm -D -t 10.0.5.97:8888 # 删除规则

# 添加服务对象 -g表示工作模式为直接路由 端口必须一致
ipvsadm -a -t 10.0.5.97:8888 -r 10.0.5.91:8888 -g
ipvsadm -a -t 10.0.5.97:8888 -r 10.0.5.92:8888 -g</code></pre>
<p>在RS机上进行操作</p>
<pre><code class="bash"># //RS1
# 绑定VIP 
ifconfig eno1:0 10.0.5.91 broadcast 10.0.5.91 netmask 255.255.240.0 up
# 添加路由
route add -host 10.0.5.91 dev eno1:0

#//RS2 同</code></pre>
<p>lvs搭建成功后查看搭建状态</p>
<pre><code class="bash">[root@lvs-web1 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -&gt; RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.5.97:8888 rr
  -&gt; 10.0.5.90:8888               Route   1      0          0
  -&gt; 10.0.5.91:8888               Route   1      0          0 </code></pre>
<p>当你访问VIP地址的时候，VIP会指向其所服务的两个真实IP地址 在轮询模式下，一定时间后指向另一个ip</p>
<p><font color='red'>单节点lvs可能会出现错误，需要使用Keepalive+lvs</font></p>
<h2 id="仅使用keepalive"><a href="#仅使用keepalive" class="headerlink" title="仅使用keepalive"></a>仅使用keepalive</h2><p>不使用RS服务器，仅使用两台DS服务器<br>测试vip在两台DS之间保持活性的过程<br>目的—-&gt; 保证vip的活性</p>
<pre><code class="text">vip 10.0.5.96

DS1: 10.0.5.93
DS2: 10.0.5.90  </code></pre>
<p>在DS-Master机器上操作</p>
<pre><code class="bash"># 安装keepalived
yum install keepalived
# 修改配置文件
! Configuration File for keepalived


global_defs {  //全局配置
   notification_email {     //通知邮件， 当keepalived中master和backup身份改变的是否会发送邮件  
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL_master   // 集群名字
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {   //vrrp实例 +  名字(同集群名字需要相同)
    state MASTER   // 当前机器身份 只能有一个master
    interface enp10s0  //当前网络接口
    virtual_router_id 51 // 虚拟路由序号 主从需要相同
    priority 100  // 优先级别 主&gt; 从
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {  //虚拟ip
        10.0.5.96/20
    }
}
</code></pre>
<p>在DS-Backup机器上的操作</p>
<pre><code class="bash"># 安装keepalived
yum install keepalived
# 修改配置文件
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP   // DS-backup机器
    interface eno1 
    virtual_router_id 51
    priority 10  //优先级别
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.5.96/20
    }
} </code></pre>
<p>主从机器启动keepalived后</p>
<p><img src="http://img.noback.top/2020-01-05-18-04-33.png" alt="2020-01-05-18-04-33"><br><img src="http://img.noback.top/2020-01-05-18-04-56.png" alt="2020-01-05-18-04-56"></p>
<p>可以发现现在的vip是在DS-master上的，<br>为了验证master主机出现错误的时候，vip会漂移到DS-backup的现象，我们可以在master关闭keepalived<br><img src="http://img.noback.top/2020-01-05-18-05-44.png" alt="2020-01-05-18-05-44"><br><img src="http://img.noback.top/2020-01-05-18-06-06.png" alt="2020-01-05-18-06-06"><br>以上，当master出现错误的时候，backup机器上出现vip，从而保证vip的活性，以此来保证lvs的稳定性</p>
<p><font color='blue'>另外，当master机器再次重启的时候，vip又会飘回到master身上</font></p>
<h2 id="1-keepalive-lvs-主从方式高可用"><a href="#1-keepalive-lvs-主从方式高可用" class="headerlink" title="1.keepalive+lvs 主从方式高可用"></a>1.keepalive+lvs 主从方式高可用</h2><p>需要准备四台服务器，均开启httpd服务<br><img src="http://img.noback.top/2020-01-05-13-15-33.png" alt="2020-01-05-13-15-33"><br><img src="http://img.noback.top/2020-01-05-13-15-53.png" alt="2020-01-05-13-15-53"><br><img src="http://img.noback.top/2020-01-05-13-16-08.png" alt="2020-01-05-13-16-08"><br><img src="http://img.noback.top/2020-01-05-13-16-21.png" alt="2020-01-05-13-16-21"></p>
<p>配置</p>
<pre><code class="bash">vip 10.0.5.96
DS1: 10.0.5.90 lvs-master
DS2: 10.0.5.93 lvs-slave


RS1: 10.0.5.91 lvs-web1
RS2: 10.0.5.92 lvs-web2 
# 测试
ping lvs-master</code></pre>
<p>首先保证 90 93两台机器的keepalive搭建成功，配置参考上面的keepalived</p>
<p>master机器上配置lvs</p>
<pre><code class="bash">vi /etc/keepalived/keepalived.conf

virtual_server 10.0.5.96 8888 {  //vip
    delay_loop 3
    lb_algo rr     // 负载均衡模式 rr--轮询
    lb_kind DR     // 负载均衡连接形式  直连路由
    !persistence_timeout 50 
    protocol TCP 

    real_server 10.0.5.91 8888 {
        weight 1
        TCP_CHECK {
            connect_port 8888  //连接端口
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 10.0.5.92 8888 {
        weight 1
        TCP_CHECK {
            connect_port 8888
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}</code></pre>
<p>backup机器上配置lvs</p>
<pre><code class="bash">vi /etc/keepalived/keepalived.conf

virtual_server 10.0.5.96 8888 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    !persistence_timeout 50
    protocol TCP

    real_server 10.0.5.91 8888 {
        weight 1
        TCP_CHECK {
            connect_port 8888
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 10.0.5.92 8888 {
        weight 1
        TCP_CHECK {
            connect_port 8888
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}</code></pre>
<p>RS机器上配置<br><font color='red'>只要是作为lvs服务的对象，这些步骤都要做，包括后面如果我们把ds-master机器在作为keepalived调度机的同时，也提供web服务的时候，就需要做这些步骤</font></p>
<pre><code class="bash"># 绑定VIP
ifconfig lo:0 10.0.5.96 netmask 255.255.240.0 broadcast 10.0.5.96 up 
# 添加路由
route add -host 10.0.5.96 dev lo:0
# 抑制arp
[root@realserver-1 ~]#echo 2 &gt; /proc/sys/net/ipv4/conf/all/arp_announce  
[root@realserver-1 ~]#echo 2 &gt; /proc/sys/net/ipv4/conf/lo/arp_announce
[root@realserver-1 ~]#echo 1 &gt; /proc/sys/net/ipv4/conf/all/arp_ignore
[root@realserver-1 ~]#echo 1 &gt;/proc/sys/net/ipv4/conf/lo/arp_ignore  

# 永久抑制arp
vi /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2</code></pre>
<p>同时启动keepalived，查看lvs连接状态</p>
<pre><code class="bash">systemctl start keepalived
ipvsadm -ln
[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -&gt; RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.5.96:8888 rr
  -&gt; 10.0.5.91:8888               Route   1      0          0
  -&gt; 10.0.5.92:8888               Route   1      0          0</code></pre>
<pre><code class="bash">while 1; do curl http://10.0.5.96:8888/|grep &quot;&lt;h2&gt;.*&lt;/h2&gt;&quot; ;sleep 1; done
# 检测状态 </code></pre>
<p><img src="http://img.noback.top/2020-01-06-14-48-22.png" alt="2020-01-06-14-48-22"></p>
<h2 id="双主方式"><a href="#双主方式" class="headerlink" title="双主方式"></a>双主方式</h2><p><a href="https://www.cnblogs.com/f-ck-need-u/p/8492298.html#2-keepalived-lvs-" target="_blank" rel="noopener">https://www.cnblogs.com/f-ck-need-u/p/8492298.html#2-keepalived-lvs-</a></p>
<h2 id="sorry-server-和local-RS"><a href="#sorry-server-和local-RS" class="headerlink" title="sorry_server 和local RS"></a>sorry_server 和local RS</h2><p>如果RS都在同一刻down掉了的话，外界就无法访问网站了。因为vip已经没有了RS的去处，这里提供了两种解决方法</p>
<ol>
<li>使用keepalived来配置一个服务页面。例如告诉外界客户端网站正在维护状态.<br>使用sorry_server 来指向 某一个ip+端口, 因为是在所有RS都宕机的情况下sorry server提供的临时服务才生效，因此通常将sorry server配置在virtual_server中而非real_server中。</li>
</ol>
<p>需要在master和backup节点上都配置sorry_server</p>
<pre><code class="bash">virtual_server 192.168.100.10 8888 {
        delay_loop 6
        lb_algo wrr
        lb_kind DR
        protocol TCP

        sorry_server 127.0.0.1 8888

# 也可以指向本地，需要开启httpd服务</code></pre>
<p>开启keepalived 然后将两台RS的机器上的httpd服务全部停掉，会发现vip会漂移到两台DS中的其中一台上</p>
<ol start="2">
<li><p>使用local RS<br>对于集群系统不大的情况下，DS 一般会比较空闲，这样就比较浪费资源。这时通常会将LVS Director自身也作为一个RS，一边提供web服务，一边提供调度功能，不过应该将它的调度权重设置低一点，以免影响负载均衡的性能。这称为local RS，local RS的RIP可以写Director上的任意地址(127.0.0.1都可以)。例如：</p>
<pre><code class="bash">virtual_server 192.168.100.10 8888 {
 ....

 real_server 127.0.0.1 8888 {
     weight 1
     TCP_CHECK {
             connect_port 8888
             connect_timeout 1
             nb_get_retry 2
             delay_before_retry 1
     }
}
} </code></pre>
</li>
</ol>
<p><font color='blue'>两个不应该同时配置，因为如果local RS 坏了，sorry server肯定无法调度</font>
　</p>
<h2 id="报错解决方案"><a href="#报错解决方案" class="headerlink" title="报错解决方案"></a>报错解决方案</h2><p>报错信息在/usr/log/messages下面</p>
<ol>
<li>主服务器停止后，备用服务没有启用<br>监控主服务器上的日志<br>Jun 28 09:18:32 rust Keepalived_vrrp: receive an invalid ip number count<br>associated with VRID!<br>Jun 28 09:18:32 rust Keepalived_vrrp: bogus VRRP packet received on eth0 !!!<br>Jun 28 09:18:32 rust Keepalived_vrrp: VRRP_Instance(VI_1) Dropping received<br>VRRP packet.HA</li>
</ol>
<p>解决方案：<br>改变配置文件/etc/keepalived/keepalived.conf 中virtual_route_id的值<br>virtual_router_id 60 主从方都要改，默认为51</p>
<ol start="2">
<li>lvs默认超时时间过程，导致框架已经搭建成功，但是效果看不出来<br>900 120 300这三个数值分别是TCP TCPFINUDP的时间.也就是说一条tcp的连接经过lvs后,lvs会把这台记录保存15分钟，就是因为这个时间过长，所以大部分人都会发现做好LVS DR之后轮询现象并没有发生，而且我也看到大部分的教程是没有说明这一点的，巨坑！！！！！！因为是实验性质，所以将此数值调整为非常小，使用以下命令调整<br>在两台DS服务器上修改<pre><code class="bash">[root@DR1 keepalived]# ipvsadm -L --timeout
Timeout (tcp tcpfin udp): 900 120 300 
[root@DR1 ~]# ipvsadm --set 1 2 1</code></pre>
</li>
</ol>
<h2 id="概念篇"><a href="#概念篇" class="headerlink" title="概念篇"></a>概念篇</h2><p>Keepalived软件起初是专为LVS负载均衡软件设计的，用来管理并监控LVS集群系统中各个服务节点的状态，后来又加入了可以实现高可用（HA）的VRRP功能。<br>于是keepalived除了能够管理LVS软件外，还可以作为其他服务（例如：Nginx、Haproxy、MySQL等）的高可用解决方案软件</p>
<p>Keepalived软件主要是通过VRRP协议实现高可用功能的。 官网<a href="https://www.keepalived.org/" target="_blank" rel="noopener">https://www.keepalived.org/</a></p>
<p>主要功能<br>–&gt; 管理LVS负载均衡<br>–&gt; 实现LVS集群节点的健康检查<br>–&gt; 作为系统网络服务的高可用性（failover）</p>
<p>运行流程<br>在 Keepalived服务正常工作时，主 Master节点会不断地向备节点发送（多播的方式）心跳消息，用以告诉备Backup节点自己还活看，当主 Master节点发生故障时，就无法发送心跳消息，备节点也就因此无法继续检测到来自主 Master节点的心跳了，于是调用自身的接管程序，接管主Master节点的 IP资源及服务。而当主 Master节点恢复时，备Backup节点又会释放主节点故障时自身接管的IP资源及服务，恢复到原来的备用角色。</p>
<h3 id="配置文件说明"><a href="#配置文件说明" class="headerlink" title="配置文件说明"></a>配置文件说明</h3><p>全局配置，一般保留路由标识信息就可以了</p>
<pre><code class="bash">global_defs {    #全局配置

    notification_email {   #定义报警邮件地址
      acassen@firewall.loc
      failover@firewall.loc  # 收件人
      sysadmin@firewall.loc
    } 
    notification_email_from Alexandre.Cassen@firewall.loc  #定义发送邮件的地址
    smtp_server 192.168.200.1   #邮箱服务器 
    smtp_connect_timeout 30      #定义超时时间
    router_id LVS_DEVEL        #定义路由标识信息，相同局域网唯一
 } </code></pre>
<p>虚拟ip配置 brrp</p>
<pre><code class="bash">vrrp_instance VI_1 {   #定义实例
    state MASTER         #状态参数 master/backup 只是说明
    interface eth0       #虚IP地址放置的网卡位置
    virtual_router_id 51 #vrrp_instance的唯一标识
    priority 100         # keepalived权重,数值越大权重越大,MASTER应大于BACKUP
    advert_int 1        #发送心跳间隔,如果backup1秒收不到心跳就接管,单位是秒
    authentication {     # ↓
        auth_type PASS    #↓
        auth_pass 1111    #认证
    }                        #↑ 
    virtual_ipaddress {  #↓
        192.168.200.16    #vip
        192.168.200.17
        192.168.200.18
    }
    nopreempt # 设置不抢占功能 
              #nopreempt 表示主节点故障恢复后不再切回到主节点，让服务一直在备用节点下工作，直到备用节点出现故障才会进行切换。
    preemtp_delay 300  # 设置抢占延时时间，单位是秒。
}  </code></pre>
<p>lvs配置</p>
<pre><code class="bash">#相当于 ipvsadm -A -t 192.168.0.89:80 -s wrr 
virtual_server 192.168.0.89 80 {
    delay_loop 6             #服务健康检查周期,单位是秒
    lb_algo wrr                 #调度算法
    lb_kind DR                 #模式
    nat_mask 255.255.255.0   
    persistence_timeout 50   #回话保持时间,单位是秒
    protocol TCP             #TCP协议转发

    sorry_server &lt;IPADDR&gt; &lt;PORT&gt; # 当所有 real server 失效后，指定的 Web 服务器的虚拟主机地址。
#添加后端realserver
#相当于 ipvsadm -a -t 192.168.0.89:80 -r 192.168.0.93:80 -w 1
    real_server 192.168.0.93 80  {    #realserver的真实IP
        weight 1                      #权重
        #健康检查
        TCP_CHECK {
            connect_timeout 8         #超时时间
            nb_get_retry 3            #重试次数
            delay_before_retry 3      #重试间隔
            connect_port 80           #检查realserver的80端口,如果80端口没监听,就会从集群中剔除
        }
    }
    real_server 192.168.0.94 80  {
        weight 1
        TCP_CHECK {
           connect_timeout 8
           nb_get_retry 3
           delay_before_retry 3
           connect_port 80
        }
    }

} </code></pre>
<h2 id="ipvsadm-概念篇"><a href="#ipvsadm-概念篇" class="headerlink" title="ipvsadm 概念篇"></a>ipvsadm 概念篇</h2><pre><code class="bash">添加虚拟服务器
    语法:ipvsadm -A [-t|u|f]  [vip_addr:port]  [-s:指定算法]
    -A:添加
    -t:TCP协议
    -u:UDP协议
    -f:防火墙标记
    -D:删除虚拟服务器记录
    -E:修改虚拟服务器记录
    -C:清空所有记录
    -L:查看
添加后端RealServer
    语法:ipvsadm -a [-t|u|f] [vip_addr:port] [-r ip_addr] [-g|i|m] [-w 指定权重]
    -a:添加
    -t:TCP协议
    -u:UDP协议
    -f:防火墙标记
    -r:指定后端realserver的IP
    -g:DR模式
    -i:TUN模式
    -m:NAT模式
    -w:指定权重
    -d:删除realserver记录
    -e:修改realserver记录
    -l:查看
通用:
    ipvsadm -ln:查看规则
    service ipvsadm save:保存规则 </code></pre>
<p><font color='red'>lvs的几种调度算法</font></p>
<ul>
<li>RR：roundrobin<br>轮询,后端RS均摊所有的请求</li>
<li>WRR weighted RR<br>加权轮询，根据权值来分配请求的数量</li>
<li>SH：Source Hashing</li>
<li>DH：Destination Hashing</li>
<li>LC：least connections</li>
<li>WLC：Weighted LC</li>
</ul>
<p><a href="https://blog.51cto.com/191226139/2090128" target="_blank" rel="noopener">具体的调度算法</a></p>
<h3 id="其他概念"><a href="#其他概念" class="headerlink" title="其他概念"></a>其他概念</h3><p><font color='red'>什么是VRRP</font><br>VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议）的缩写，VRRP出现的目的就是为了解决静态路由单点故障问题的，它能够保证当个别节点宕机时，整个网络可以不间断地运行。VRRP通过一种竞选机制来将路由的任务交给某台VRRP路由器的。<br><a href="https://www.cnblogs.com/f-ck-need-u/p/8483807.html" target="_blank" rel="noopener">https://www.cnblogs.com/f-ck-need-u/p/8483807.html</a></p>
<h2 id="网址"><a href="#网址" class="headerlink" title="网址"></a>网址</h2><p><a href="https://www.cnblogs.com/clsn/p/8052649.html#auto_id_4" target="_blank" rel="noopener">https://www.cnblogs.com/clsn/p/8052649.html#auto_id_4</a><br><a href="https://www.cnblogs.com/clsn/p/7920637.html#_label7" target="_blank" rel="noopener">https://www.cnblogs.com/clsn/p/7920637.html#_label7</a><br><a href="https://www.jianshu.com/p/4b46586e79aa" target="_blank" rel="noopener">https://www.jianshu.com/p/4b46586e79aa</a><br><a href="https://blog.51cto.com/dengaosky/2129856" target="_blank" rel="noopener">https://blog.51cto.com/dengaosky/2129856</a><br><a href="https://www.cnblogs.com/hanshanxiaoheshang/p/10117152.html" target="_blank" rel="noopener">https://www.cnblogs.com/hanshanxiaoheshang/p/10117152.html</a></p>
<p><a href="https://andy0570.com/2019/04/03/%E5%9F%BA%E4%BA%8E%20LVS%20+%20keepalived%20%E7%9A%84%E5%8F%8C%E6%9C%BA%E7%83%AD%E5%A4%87%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E5%99%A8/" target="_blank" rel="noopener">https://andy0570.com/2019/04/03/%E5%9F%BA%E4%BA%8E%20LVS%20+%20keepalived%20%E7%9A%84%E5%8F%8C%E6%9C%BA%E7%83%AD%E5%A4%87%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E5%99%A8/</a></p>

        </div>
        
            <ul class="post-copyright">
            <li><strong>本文标题：</strong><a href="http://alpaca-h.gitee.io/2020/01/05/blog_back_new/linux/keepalive+lvs%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/">keepalive+lvs 四层负载均衡</a></li>
            <li><strong>本文作者：</strong><a href="http://alpaca-h.gitee.io">Alpaca</a></li>
            <li><strong>本文链接：</strong><a href="http://alpaca-h.gitee.io/2020/01/05/blog_back_new/linux/keepalive+lvs%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/">http://alpaca-h.gitee.io/2020/01/05/blog_back_new/linux/keepalive+lvs%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/</a></li>
            <li><strong>发布时间：</strong>2020-01-05</li>
            <li><strong>版权声明：</strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处！
            </li>
            </ul>
        
        
        
        
    </div>
</div>





<div class="card card-transparent">
    <div class="level post-navigation is-flex-wrap is-mobile">
        
        
        <div class="level-end">
            <a class="level level-item has-link-grey  article-nav-next" href="/2020/01/05/blog_back_new/linux/linux%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86/iptables%E3%80%81firewall%E9%98%B2%E7%81%AB%E5%A2%99/">
                <span class="level-item">iptables、firewall 防火墙</span>
                <i class="level-item fas fa-chevron-right"></i>
            </a>
        </div>
        
    </div>
</div>



</div>
                




<div class="column is-4-tablet is-4-desktop is-3-widescreen  has-order-1 column-left ">
    
        
<div class="card widget">
    <div class="card-content">
        <nav class="level" style="margin-bottom:1rem">
            <div class="level-item has-text-centered">
                <div>
                    
                        <img class="image is-96x96 has-mb-6" src="https://www.gravatar.com/avatar/e0f4032c0f2d1068ffffbaf93c0bef52?s=96" alt="Xinyu Liu">
                    
                    
                    <p class="is-size-4 is-block">
                        Xinyu Liu
                    </p>
                    
                    
                    <p class="is-size-6 is-block">
                        Alpha Lxy
                    </p>
                    
                    
                    <p class="is-size-6 is-flex is-flex-center has-text-grey">
                        <i class="fas fa-map-marker-alt has-mr-7"></i>
                        <span>Beijing, China</span>
                    </p>
                    
                </div>
            </div>
        </nav>
        <nav class="level menu-list is-mobile" style="margin-bottom:1rem">
            <div class="level-item has-text-centered is-marginless">
                <a href="/archives/">
                    <p class="heading">
                        Posts
                    </p>
                    <p class="title has-text-weight-normal">
                        40
                    </p>
                </a>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <a href="/categories/">
                    <p class="heading">
                        Categories
                    </p>
                    <p class="title has-text-weight-normal">
                        13
                    </p>
                </a>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <a href="/tags/">
                    <p class="heading">
                        Tags
                    </p>
                    <p class="title has-text-weight-normal">
                        0
                    </p>
                </a>
            </div>
        </nav>
        <div class="level">
            <a class="level-item button is-link is-rounded" href="https://www.github.com/AlphaLxy" target="_blank">
                <i class="fab fa-github"></i>&nbsp;&nbsp;Follow</a>
        </div>
        
        
    </div>
</div>

    
        
<div class="card widget column-left is-sticky" id="toc">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                Catalogue
            </h3>
            <ul class="menu-list"><li>
        <a class="is-flex" href="#关键词">
        <span class="has-mr-6">1</span>
        <span>关键词</span>
        </a></li><li>
        <a class="is-flex" href="#仅使用lvs">
        <span class="has-mr-6">2</span>
        <span>仅使用lvs</span>
        </a></li><li>
        <a class="is-flex" href="#仅使用keepalive">
        <span class="has-mr-6">3</span>
        <span>仅使用keepalive</span>
        </a></li><li>
        <a class="is-flex" href="#1-keepalive-lvs-主从方式高可用">
        <span class="has-mr-6">4</span>
        <span>1.keepalive+lvs 主从方式高可用</span>
        </a></li><li>
        <a class="is-flex" href="#双主方式">
        <span class="has-mr-6">5</span>
        <span>双主方式</span>
        </a></li><li>
        <a class="is-flex" href="#sorry-server-和local-RS">
        <span class="has-mr-6">6</span>
        <span>sorry_server 和local RS</span>
        </a></li><li>
        <a class="is-flex" href="#报错解决方案">
        <span class="has-mr-6">7</span>
        <span>报错解决方案</span>
        </a></li><li>
        <a class="is-flex" href="#概念篇">
        <span class="has-mr-6">8</span>
        <span>概念篇</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#配置文件说明">
        <span class="has-mr-6">8.1</span>
        <span>配置文件说明</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#ipvsadm-概念篇">
        <span class="has-mr-6">9</span>
        <span>ipvsadm 概念篇</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#其他概念">
        <span class="has-mr-6">9.1</span>
        <span>其他概念</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#网址">
        <span class="has-mr-6">10</span>
        <span>网址</span>
        </a></li></ul>
        </div>
    </div>
</div>


    
    
        <div class="column-right-shadow is-hidden-widescreen ">
        
        </div>
    
</div>

                
            </div>
        </div>
    </section>
    <footer class="footer">
    <div class="container">
        <div class="level">
            <div class="level-start has-text-centered-mobile">
                <a class="footer-logo is-block has-mb-6" href="/">
                
                    <img src="/images/logo.svg" alt="keepalive+lvs 四层负载均衡" height="28">
                
                </a>
                <p class="is-size-7">
                &copy; 2020 Alpaca&nbsp;
                Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> & <a
                        href="http://github.com/ppoffice/hexo-theme-icarus" target="_blank">Icarus</a>
                
                <br>
                <span id="busuanzi_container_site_uv">
                Visited by <span id="busuanzi_value_site_uv">0</span> users
                </span>
                
                </p>
            </div>
            <div class="level-end">
            
                <div class="field has-addons is-flex-center-mobile has-mt-5-mobile is-flex-wrap is-flex-middle">
                
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
                        
                        <i class="fab fa-creative-commons"></i>&nbsp;<i class="fab fa-creative-commons-by"></i>&nbsp;<i class="fab fa-creative-commons-nc"></i>&nbsp;<i class="fab fa-creative-commons-sa"></i>&nbsp;
                        
                    </a>
                </p>
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="AlphaLxy GitHub" href="https://www.github.com/AlphaLxy">
                        
                        <i class="fab fa-github"></i>&nbsp;
                        
                    </a>
                </p>
                
                </div>
            
            </div>
        </div>
    </div>
</footer>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script>
<script>moment.locale("en");</script>


    
    
    
    <script src="/js/animation.js"></script>
    

    
    
    
    <script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer></script>
    <script src="/js/gallery.js" defer></script>
    

    
    

<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/" target="_blank" rel="noopener">Update
            my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer></script>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        });
    });
</script>


    
    
<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    MathJax.Hub.Config({
        'HTML-CSS': {
            matchFontHeight: false
        },
        SVG: {
            matchFontHeight: false
        },
        CommonHTML: {
            matchFontHeight: false
        },
        tex2jax: {
            inlineMath: [
                ['$','$'],
                ['\\(','\\)']
            ]
        }
    });
});
</script>

    
    

<a id="back-to-top" title="Back to Top" href="javascript:;">
    <i class="fas fa-chevron-up"></i>
</a>
<script src="/js/back-to-top.js" defer></script>


    
    

    
    
    
    

    
    
    
    
    
    <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer></script>
    <script src="/js/clipboard.js" defer></script>
    

    
    
    

    


<script src="/js/main.js" defer></script>

    
    <div class="searchbox ins-search">
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="Type something..." />
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: 'Posts',
                PAGES: 'Pages',
                CATEGORIES: 'Categories',
                TAGS: 'Tags',
                UNTITLED: '(Untitled)',
            },
            CONTENT_URL: '/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/js/insight.js" defer></script>
<link rel="stylesheet" href="/css/search.css">
<link rel="stylesheet" href="/css/insight.css">
    
</body>
</html>